package com.thirdgroup.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@WebListener
public class FileBackupListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("WebListener   contextInitialized ");
        // 应用程序启动时的初始化操作   获取服务器中upload文件夹路径
        String realPath = sce.getServletContext().getRealPath("/uploads/");
        System.out.println("/upload/    realPath="+realPath);
        //这是绝对路径，根据自己项目路径进行修改
        String realPath1 = "D:/idea-code/gxa-teleCom/src/main/resources/static/imgs/";
        File file = new File(realPath);
        file.mkdirs();
        restoreFiles(realPath,realPath1);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("WebListener   contextDestroyed ");
        // 应用程序关闭时的清理操作  获取服务器中upload文件夹路径
        String realPath = sce.getServletContext().getRealPath("/uploads/");
        System.out.println("/upload/  realPath="+realPath);
        //这是绝对路径，根据自己项目路径进行修改
        String realPath1 = "D:/idea-code/gxa-teleCom/src/main/resources/static/imgs/";
        System.out.println("realPath1="+realPath1);
        File file = new File(realPath);
        file.mkdirs();
        backupFiles(realPath,realPath1);


    }

    private void backupFiles(String realPath,String real) {
        String sourceDirectoryPath = realPath; // 上传文件存储的目录
        String destinationDirectoryPath = real ; // 静态资源目录

        try {
            // 复制文件到静态资源目录
            Files.walk(new File(sourceDirectoryPath).toPath())
                    .filter(Files::isRegularFile)
                    .forEach(source -> {
                        Path destination = new File(destinationDirectoryPath, source.getFileName().toString()).toPath();
                        try {
                            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void restoreFiles(String realPath,String real) {
        String uploadDirectoryPath = realPath;
        String staticDirectoryPath = real;

        try {
            Files.walk(Paths.get(staticDirectoryPath))
                    .filter(Files::isRegularFile)
                    .forEach(filePath -> {
                        try {
                            Path destination = Paths.get(uploadDirectoryPath + filePath.getFileName());
                            Files.copy(filePath, destination, StandardCopyOption.REPLACE_EXISTING);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

